<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="证照名称" prop="title">
						<el-input v-model="form.title" placeholder="证照名称" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="证照编号" prop="codeNo">
						<el-input v-model="form.codeNo" placeholder="证照编号" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="关联企业" prop="entId">
						<el-select v-model="form.entId" clearable filterable :remote-method="remoteMethod" style="width: 100%" placeholder="输入公司名称、或统一信用代码、公司编号进行查询选择">
							<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="证照类型" prop="licType">
						<sc-select v-model="form.licType" :params="{code:'licType'}" :apiObj="$API.invite.inviteDic.getInviteDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="生效日期" prop="beginTime">
						<el-date-picker v-model="form.beginTime" placeholder="生效日期" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="截止日期" prop="endTime">
						<el-date-picker v-model="form.endTime" placeholder="截止日期" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="证照备注">
				<el-input v-model="form.remark" placeholder="证照备注" clearable maxlength="380" type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="证照附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :data="{module:'invite'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import AttachMenu from '@/views/module/attachMenu'
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserDialog,
		AttachMenu
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			mode: "add",
			btName: "保存",
			titleMap: {
				add: '添加证照信息',
				edit: '编辑证照信息',
				show: '查看'
			},
			fileList: [],
			options:[],
			apiObj: this.$API.file.attach.uploadFile,
			templateList: [],
			templates: [],
			form: {
				licId: "",
				sortNo: 0,
				title: "",
				entId: "",
				codeNo: "",
				licType: "",
				status: "0",
				beginTime: "",
				endTime: "",
				attachId: "",
				remark: "",
			},
			rules: {
				codeNo: [
					{required: true, message: '证照编号不能为空', trigger: 'blur'}
				],
				licType: [
					{required: true, message: '证照类型不能为空',trigger: 'blur'}
				],
				title: [
					{required: true, message: '证照名称不能为空',trigger: 'blur'}
				],
				entId: [
					{required: true, message: '关联企业不能为空',trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.remoteMethod("");
	},
	methods: {
		async remoteMethod(query){
			let res = await this.$API.invite.inviteEnt.getInviteEntListForSelect.get({keyword:query});
			if (res.code == 200) {
				this.options = res.data
			}
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.invite.inviteEntLic.insertInviteEntLic.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.invite.inviteEntLic.updateInviteEntLic.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.licId = data.licId
			this.form.sortNo = data.sortNo
			this.form.title = data.title
			this.form.entId = data.entId
			this.form.codeNo = data.codeNo
			this.form.licType = data.licType
			this.form.status = data.status
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.attachId = data.attachId
			this.form.remark = data.remark

		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
